package com.`as`.common

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.util.DisplayMetrics
import android.view.View
import android.view.WindowManager

/**
 * descrition:屏幕相关
 */
/**
 * 获得屏幕高度
 */
fun getScreenWidth(context: Context): Int {
    val wm = context
        .getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val outMetrics = DisplayMetrics()

    wm.defaultDisplay.getMetrics(outMetrics)
    return outMetrics.widthPixels
}

/**
 * 获得屏幕宽度
 */
fun getScreenHeight(context: Context): Int {
    val wm = context
        .getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val outMetrics = DisplayMetrics()
    wm.defaultDisplay.getMetrics(outMetrics)
    return outMetrics.heightPixels
}

/**
 * 获取当前屏幕截图，包含状态栏
 */
fun snapShotWithStatusBar(activity: Activity): Bitmap? {
    val view: View = activity.window.decorView
    view.isDrawingCacheEnabled = true
    view.buildDrawingCache()
    val bmp: Bitmap = view.drawingCache
    val width = getScreenWidth(activity)
    val height = getScreenHeight(activity)
    val bp = Bitmap.createBitmap(bmp, 0, 0, width, height)
    view.destroyDrawingCache()
    return bp
}

/**
 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
 */
fun px2dip(context: Context, pxValue: Float): Int {
    val scale = context.resources.displayMetrics.density
    return (pxValue / scale + 0.5f).toInt()
}

fun dp2px(context: Context, dipValue: Float): Int {
    val scale: Float = context.resources.displayMetrics.density
    return (dipValue * scale + 0.5f).toInt()
}
